//
//  BIDBusinessRemindListViewController.m
//  cxgov
//
//  Created by mal on 16/3/15.
//  Copyright © 2016年 mal. All rights reserved.
//

#import "BIDMeetingRemindListViewController.h"
#import "BIDMeetingRemindCell.h"
#import "BIDCreateMeetingRemindViewController.h"
#import "BIDMeetingRemindDetailViewController.h"

//已收提醒列表
static NSString *receivedRemindListURL = @"meetingAndroid!gainMeetingRecListGson.action";
//已收提醒删除
static NSString *deleteReceivedRemindURL = @"meetingAndroid!dropMeetingReply.action";
//
//已发提醒列表
static NSString *sentRemindListURL = @"meetingAndroid!gainMeetingSendList.action";
//已发提醒删除
static NSString *deleteSentRemindURL = @"meetingAndroid!dropSendMeeting.action";

@interface BIDMeetingRemindListViewController ()<BIDBaseListViewDelegate>
{
    NSString *_desRequestURL;
    NSString *_desDeleteURL;
    BOOL _bRegister;
    //当前是已收列表还是已发列表
    BOOL _bReceived;
}
@end

@implementation BIDMeetingRemindListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNavigationTitleView];
    //
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(createRemind)];
    _bReceived = YES;
    //
    _desRequestURL = receivedRemindListURL;
    _desDeleteURL = deleteReceivedRemindURL;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [self.dataSourceArr removeAllObjects];
    self.curPage = 0;
    [self loadData];
}

- (void)loadData
{
    self.curPage++;
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], _desRequestURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"userId=%@&page=%ld&rows=%ld", [AppDelegate getUserId], self.curPage, self.countPerPage];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(id responseObj, NSString *errMsg){
        if(responseObj)
        {
            NSDictionary *responseDic = (NSDictionary*)responseObj;
            [self.dataSourceArr addObjectsFromArray:responseDic[@"rows"]];
            if(self.dataSourceArr.count<self.curPage*self.countPerPage)
            {
                [self.myTableView hasNoMoreData];
            }
            [self.myTableView reloadData];
        }
    }];
}

- (void)setNavigationTitleView
{
    UISegmentedControl *segmentedCtrl = [[UISegmentedControl alloc] initWithItems:@[@"已收提醒", @"已发提醒"]];
    segmentedCtrl.selectedSegmentIndex = 0;
    [segmentedCtrl addTarget:self action:@selector(segmentedCtrlSelectChanged:) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = segmentedCtrl;
}

//新建提醒
- (void)createRemind
{
    BIDCreateMeetingRemindViewController *vc = [[BIDCreateMeetingRemindViewController alloc] initWithNibName:@"BIDCreateMeetingRemindViewController" bundle:nil];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)segmentedCtrlSelectChanged:(id)sender
{
    UISegmentedControl *segmentedCtrl = (UISegmentedControl*)sender;
    NSUInteger index = segmentedCtrl.selectedSegmentIndex;
    if(index==0)
    {
        //已收提醒
        _desRequestURL = receivedRemindListURL;
        _desDeleteURL = deleteReceivedRemindURL;
        _bReceived = YES;
    }
    else
    {
        //已发提醒
        _desRequestURL = sentRemindListURL;
        _desDeleteURL = deleteSentRemindURL;
        _bReceived = NO;
    }
    [self.dataSourceArr removeAllObjects];
    self.curPage = 0;
    [self loadData];
}

//删除列表中的一条通知
- (void)deleteRemindAtIndexPath:(NSIndexPath*)indexPath
{
    NSDictionary *dic = self.dataSourceArr[indexPath.row];
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], _desDeleteURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"ids=%@", dic[@"id"]];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(id responseObj, NSString *errMsg){
        if(responseObj)
        {
            NSDictionary *responseDic = (NSDictionary*)responseObj;
            NSNumber *value = responseDic[@"isSuccess"];
            if([value intValue]==1)
            {
                [self.myTableView beginUpdates];
                [self.dataSourceArr removeObjectAtIndex:indexPath.row];
                [self.myTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
                [self.myTableView endUpdates];
            }
            else
            {
                [BIDGlobalMethods showAlertView:@"删除失败" buttonTitle:ALERT_BTN_TITLE delegate:nil tag:0];
            }
        }
    }];
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArr.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    NSUInteger row = indexPath.row;
    NSUInteger section = indexPath.section;
    static NSString *identifier = @"identifier";
    if(!_bRegister)
    {
        _bRegister = YES;
        UINib *nib = [UINib nibWithNibName:@"BIDMeetingRemindCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:identifier];
    }
    BIDMeetingRemindCell *remindCell = [tableView dequeueReusableCellWithIdentifier:identifier];
    NSDictionary *dic = self.dataSourceArr[row];
    remindCell.titleLabel.text = dic[@"title"];
    remindCell.dateLabel.text = [[NSString alloc] initWithFormat:@"时间:%@", dic[@"meetingTime"]];
    remindCell.placeLabel.text = [[NSString alloc] initWithFormat:@"地点:%@", dic[@"address"]];
    NSNumber *value = dic[@"readFlag"];
    if([value intValue] == 1)
    {
        [remindCell setReadFlag:NO];
    }
    else
    {
        [remindCell setReadFlag:YES];
    }
    cell = remindCell;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 79.f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    BIDMeetingRemindDetailViewController *vc = [[BIDMeetingRemindDetailViewController alloc] initWithNibName:@"BIDMeetingRemindDetailViewController" bundle:nil];
    vc.bReceived = _bReceived;
    NSDictionary *dic = self.dataSourceArr[indexPath.row];
    vc.remindId = dic[@"id"];
    vc.creatorId = dic[@"createId"];
    [self.navigationController pushViewController:vc animated:YES];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(editingStyle == UITableViewCellEditingStyleDelete)
    {
        [self deleteRemindAtIndexPath:indexPath];
    }
}

@end
